Python Operators and Expressions
Python provides a rich set of operators to perform various operations on data. These operators are essential for writing expressions, controlling program flow, and manipulating data. Below is a comprehensive guide to Python's operators and expressions, complete with examples and explanations.
1. Arithmetic Operators​
Arithmetic operators allow you to perform mathematical calculations on numerical values. They work on integers, floats, and (to some extent) complex numbers.
Operators:​
| Operator | Meaning | Usage |
|---|---|---|
+ | Addition | Adds two operands |
- | Subtraction | Subtracts right from left operand |
* | Multiplication | Multiplies operands |
/ | Division | Divides left by right, returns float |
% | Modulus | Divides, returns remainder |
// | Floor Division | Divides, returns integer part |
** | Exponentiation | Raises left to power of right |
Practical Examples:​
a = 15
b = 4
print(a + b) # Output: 19
print(a - b) # Output: 11
print(a * b) # Output: 60
print(a / b) # Output: 3.75
print(a % b) # Output: 3
print(a // b) # Output: 3 (15 divided by 4 is 3.75, floor is 3)
print(a ** b) # Output: 50625 (15 to the power of 4)
Note: Division / always returns a float. Use // for integer
division (flooring).
2. Comparison Operators​
Comparison operators are used to compare two values. They return a
boolean value (True or False) and are vital in decision-making
(e.g., if-else, loops).
Operators:​
| Operator | Usage | Meaning |
|---|---|---|
== | a == b | True if a equals b |
!= | a != b | True if a not equal b |
> | a > b | True if a greater than b |
< | a < b | True if a less than b |
>= | a >= b | True if a greater/equal b |
<= | a <= b | True if a less/equal b |
Example:​
a = 5
b = 10
print(a == b) # False
print(a != b) # True
print(a > b) # False
print(a < b) # True
print(a >= b) # False
print(a <= b) # True
Use Case - Conditional Statement:​
age = int(input("Enter your age: "))
if age >= 18:
print("Adult")
else:
print("Minor")
3. Logical Operators​
Logical operators allow combining multiple boolean conditions. They help create advanced conditional logic.
Operators:​
| Operator | Usage | Meaning |
|---|---|---|
and | a and b | True if both a and b are True |
or | a or b | True if at least one is True |
not | not a | True if a is False, False if a is True |
Example:​
x = True
y = False
print(x and y) # False
print(x or y) # True
print(not x) # False
Practical Examples:​
x, y = 5, 10
print(x > 3 and y < 20) # True
print(x < 3 or y < 20) # True
print(not (x == 5)) # False
Chained Use:​
user, pwd = "admin", "1234"
if user == "admin" and pwd == "1234":
print("Access Granted")
else:
print("Access Denied")
4. Assignment Operators​
Assignment operators are used to assign values to variables. Compound assignment operators combine arithmetic and assignment operations.
Operators:​
| Operator | Usage | Equivalent Statement |
|---|---|---|
= | x = y | x = y |
+= | x += y | x = x + y |
-= | x -= y | x = x - y |
*= | x *= y | x = x * y |
/= | x /= y | x = x / y |
%= | x %= y | x = x % y |
//= | x //= y | x = x // y |
**= | x **= y | x = x ** y |
Example:​
a = 10
# Compound assignments
a += 5 # a = a + 5
print(a) # 15
a -= 3
print(a) # 12
a *= 2
print(a) # 24
a /= 4
print(a) # 6.0
Practical Examples:​
score = 50
score += 10 # score now 60
score -= 20 # score now 40
score *= 2 # score now 80
score /= 4 # score now 20.0
score %= 7 # score now 6.0
Compound Use:​
counter = 1
for i in range(4):
counter *= 2
print(counter) # 16
5. Bitwise Operators​
Bitwise operators operate on the binary representations of integers. They are commonly used in low-level programming, optimization, and some mathematical tasks.
Operators:​
| Operator | Symbol | Example | Effect |
|---|---|---|---|
AND | & | a & b | 1 if bits in both |
OR | ` | ` | `a |
XOR | ^ | a ^ b | 1 if in only one |
NOT | ~ | ~a | Invert bits |
<< | << | a << n | Shift bits left n |
>> | >> | a >> n | Shift bits right n |
Example:​
a = 5 # 0b0101
b = 3 # 0b0011
print(a & b) # AND -> 1
print(a | b) # OR -> 7
print(a ^ b) # XOR -> 6
print(~a) # NOT -> -6
print(a << 1) # Left shift -> 10
print(a >> 1) # Right shift -> 2
Practical Examples:​
a = 5 # 0b0101
b = 3 # 0b0011
print(a & b) # 1 (0b0001)
print(a | b) # 7 (0b0111)
print(a ^ b) # 6 (0b0110)
print(~a) # -6 (inverts: -(a+1))
print(a << 2) # 20 (shift left 2 bits: 0b010100)
print(b >> 1) # 1 (0b0001)
Use Case - Masking:​
pixel = 0b11100101
mask = 0b00001111
result = pixel & mask # Extract lower 4 bits
print(bin(result)) # 0b00000101
6. Identity and Membership Operators​
Identity Operators:​
Identity operators check if two variables reference the same object in memory (not just equal content).
| Operator | Usage | Meaning |
|---|---|---|
is | a is b | True if same obj |
is not | a is not b | True if not same obj |
Example:​
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # True
print(a is c) # False
Membership Operators:​
Membership operators check if a value exists in a sequence (e.g., list, string).
| Operator | Usage | Meaning |
|---|---|---|
in | value in sequence | True if exists |
not in | value not in sequence | True if not exists |
Example:​
fruits = ["apple", "banana"]
print("apple" in fruits) # True
print("cherry" not in fruits) # True
7. Type Casting​
Type casting allows you to convert a variable from one data type to another. It is essential for working with mixed data types.
Functions:​
| Function | Converts to | Example |
|---|---|---|
int() | Integer | int("42") |
float() | Float | float("3.14") |
str() | String | str(3.14) |
bool() | Boolean | bool(1), bool("") |
list() | List | list("abc") |
tuple() | Tuple | tuple([1,2,3]) |
set() | Set | set("apple") |
Practical Examples:​
# String to int and float
num1 = "100"
print(int(num1) + 1) # 101
num2 = "3.14159"
print(float(num2) * 2) # 6.28318
# Int/float to string
price = 45.99
print("Price: " + str(price)) # "Price: 45.99"
# List to tuple
colors = ["red", "green", "blue"]
tup = tuple(colors)
print(tup) # ('red', 'green', 'blue')
# Sequence to set (removes duplicates)
letters = "banana"
print(set(letters)) # {'b', 'a', 'n'}
# Boolean conversion
print(bool(0)) # False
print(bool("abc")) # True
print(bool("")) # False
# Input conversion
age = int(input("Enter your age: ")) # Converts input to integer
Note: Invalid type conversion (e.g., int("abc")) raises a
ValueError.
Conclusion​
This document provides a detailed overview of Python's operators and expressions, including arithmetic, comparison, logical, assignment, bitwise, identity, membership, and type casting. Understanding these operators is crucial for writing efficient and effective Python programs. By mastering these concepts, you'll be able to manipulate data, control program flow, and solve complex problems with ease.